/*
 * DailyQuotationPresenter.java
 * business
 *
 * Created by ChenTao on 2017/6/5.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.dailyquotation;

import com.yidiandao.business.data.bean.ShareEntity;
import com.yidiandao.business.data.model.DailyQuotationModel;
import com.yidiandao.business.data.model.IDailyQuotationModel;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.List;

/**
 * 每日行情
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017-06-05 9:44
 */
public class DailyQuotationPresenter implements DailyQuotationContract.Presenter {

    private DailyQuotationContract.View mQuotationView;

    private IDailyQuotationModel mQuotationModel;

    private ShareEntity mShareEntity;

    public DailyQuotationPresenter(DailyQuotationContract.View view) {
        mQuotationView = view;
        mQuotationModel = new DailyQuotationModel();

        mQuotationView.setPresenter(this);
    }

    @Override
    public void start() {
        loadDailyData(true);
    }

    @Override
    public void loadDailyData(final boolean isRefreshing) {

        mQuotationModel.loadDailyData(isRefreshing, mQuotationView.getPagerTag(), new IDailyQuotationModel.OnAccessNetListener<List<Object>>() {
            @Override
            public void onSuccess(List<Object> objects, boolean emptyResult, boolean hasMoreData, ShareEntity shareEntity) {
                if (isRefreshing) {
                    mQuotationView.refreshContent(mQuotationView.checkResultObject(objects));
                    mQuotationView.setRefreshComplete();
                }
                mQuotationView.setItems(objects);
                mQuotationView.loadMoreFinish(emptyResult, hasMoreData);

                mShareEntity = shareEntity;
            }

            @Override
            public void onFailed() {
                if (isRefreshing) {
                    mQuotationView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                } else {
                    mQuotationView.loadMoreError();
                }
            }
        });

    }

    @Override
    public ShareEntity getShareEntity() {
        return mShareEntity;
    }
}
